package com.example.lenovo.sbns.Activity;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.example.lenovo.sbns.R;
import com.example.lenovo.sbns.Register;

import java.util.List;


public class themePage extends Fragment {

    private EditText userName,passWord;
    private Button login,register;
    private TextView error;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        System.out.println("07");
        View view = inflater.inflate(R.layout.fragment_theme_page, container, false);
        System.out.println("06");

        userName = (EditText) view.findViewById(R.id.UserName);
        passWord = (EditText) view.findViewById(R.id.PassWord);
        login = (Button) view.findViewById(R.id.Login);
        register = (Button) view.findViewById(R.id.Register);
        error = (TextView) view.findViewById(R.id.Error);

        login.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(CheckInfo()){
                            login();
                        }
                    }
                }
        );

        register.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.setClass(getActivity(), Register.class);
                        startActivity(intent);
                    }
                }
        );

        return view;
    }

    /**
     * 非空验证函数
     */
    public boolean CheckInfo(){
        if(userName.getText().toString().length()==0){
            error.setText("用户名不能为空！");
            return false;
        }
        if(passWord.getText().toString().length() == 0){
            error.setText("密码不能为空");
            return false;
        }
        return true;
    }

    /**
     * 登录函数
     */
    public void login(){

        String username = userName.getText().toString();
        String password = passWord.getText().toString();

        AVQuery<AVObject> query = new AVQuery<>("UserTable");
        query.whereEqualTo("username", username);
        query.whereEqualTo("password", password);
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(list.size() == 0){
                    error.setText("用户名或密码错误！");
                }
                else {
                    indexPage indexPage = new indexPage();
                    FragmentManager fragmentManager = getFragmentManager();
                    FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
                    beginTransaction.replace(R.id.FrameOut, indexPage);
                    beginTransaction.commit();
                }
            }
        });
    }


}
